package com.test.admin.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;


public class QuartzManager {

    private static SchedulerFactory schedulerFactory = new StdSchedulerFactory();

    public static void addJob(String jobName, String jobGroup, String triggerName, String triggerGroup, Class jobClass, String cron) {
        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            TriggerBuilder builder = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroup);
            builder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
            builder.startNow();//调度任务时立即执行
            Trigger trigger = builder.build();
            JobDetail job = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroup).build();//任务实例
            scheduler.scheduleJob(job, trigger);//绑定触发器和任务，用trigger指定的时间规则，执行job
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    public static void modifyJobTime(String jobName, String jobGroup, String triggerName, String triggerGroup, String cron) {
        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroup);
            CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);//根据分组和名字获取原来的trigger
            if (trigger == null) {
                return;
            }
            String oldTime = trigger.getCronExpression();//原来的表达式
            if (!oldTime.equals(cron)) {//新表达式和原来不一样就去修改
                //创建一个新的trigger
                TriggerBuilder builder = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroup);
                builder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
                builder.startNow();//调度任务时立即执行
                trigger = (CronTrigger) builder.build();
                scheduler.rescheduleJob(triggerKey, trigger);//修改一个触发器的时间
            }
        } catch (SchedulerException e) {
            e.printStackTrace();

        }
    }

    public static void removeJob(String jobName, String jobGroup, String triggerName, String triggerGroup) {
        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroup);
            scheduler.pauseTrigger(triggerKey);//停止触发器
            scheduler.unscheduleJob(triggerKey);//移除触发器
            scheduler.deleteJob(JobKey.jobKey(jobName, jobGroup));//删除任务
        } catch (SchedulerException e) {
            e.printStackTrace();

        }
    }

    public static void pauseJob(String jobName, String jobGroup) {
        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            scheduler.pauseJob(JobKey.jobKey(jobName, jobGroup));//暂停一个任务
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    public static void resumeJob(String jobName, String jobGroup) {
        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            scheduler.resumeJob(JobKey.jobKey(jobName, jobGroup));//恢复一个任务
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    public static Integer getJobStatus(String triggerName, String triggerGroup) {
        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            Trigger.TriggerState state = scheduler.getTriggerState(TriggerKey.triggerKey(triggerName, triggerGroup));
            return state.ordinal();//2是暂停
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return -7; //未知
    }
}
